package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pojo.Test;
import com.service.TestService;
import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.Map;

@RestController
@RequestMapping("/test")
@CrossOrigin("*")
public class TestAction {

    @Autowired
    private TestService testService;

    /**
     * 新增试卷
     *
     * @param test 页面传的参数
     * @return
     */
    @RequestMapping("/addTest")
    public ResponseBean addTest(Test test) {
        // 调用 TestService 中新增试卷
        boolean n = testService.save(test);
        // true 或 false
        return new ResponseBean(0, 0, n);
    }

    /**
     * 分页查询所有可用试卷
     *
     * @param page  第几条
     * @param limit 一页几条
     * @param m     搜索条件
     * @return
     */
    @RequestMapping("/searchTest")
    public ResponseBean searchTest(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map m) {
        // 调用 TestService 中分页查询所有可用试卷
        IPage iPage = testService.searchTest(page, limit, m);
        System.out.println(iPage.getRecords());
        return new ResponseBean(0, iPage.getTotal(), iPage.getRecords());
    }

    @RequestMapping("/searchGrande")
    public ResponseBean searchGrande(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map m) {
        // 调用 TestService 中分页查询所有可用试卷
        IPage iPage = testService.searchGrande(page, limit, m);
        return new ResponseBean(0, iPage.getTotal(), iPage.getRecords());
    }



    /**
     * 根据id修改试卷名
     *
     * @param m 参数
     * @return 大于零则成功
     */
    @RequestMapping("/updateTest")
    public ResponseBean updateTest(@RequestParam Map m) {
        int n = testService.updateTest(m);
        return new ResponseBean(0, 0, n);
    }

    /**
     * 根据id删除试卷
     *
     * @param id 试卷id
     * @return true 或 false
     */
    @RequestMapping("/removeTestById")
    public ResponseBean removeTestById(Integer id){
        boolean n = testService.removeById(id);
        return new ResponseBean(0,0,n);
    }
}
